﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyCompany.MyProject.Common.HttpContextUser;
using MyCompany.MyProject.IService;
using MyCompany.MyProject.Model.DTO;
using MyCompany.MyProject.Model.Models;
using MyCompany.MyProject.Model.Models.Tenants;
using SqlSugar;

namespace MyCompany.MyProject.WebApi.Controllers
{
    /// <summary>
    /// 测试多租户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Common.GlobalVaribale.Permission.Name)]
    public class TenantController : ControllerBase
    {
        private readonly IUser user;
        private readonly IBaseService<BusinessTable> bizService;
        private readonly IBaseService<MultiBusinessTable> multiBusinessService;
        private readonly IBaseService<SubLibraryBusinessTable> subLibBusinessService;
        private readonly IBaseService<SysTenant> sysTenantService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="user"></param>
        /// <param name="bizService"></param>
        /// <param name="multiBusinessService"></param>
        /// <param name="subLibBusinessService"></param>
        /// <param name="sysTenantService"></param>
        public TenantController(IUser user,IBaseService<BusinessTable> bizService,
            IBaseService<MultiBusinessTable> multiBusinessService,
            IBaseService<SubLibraryBusinessTable> subLibBusinessService,
            IBaseService<SysTenant> sysTenantService)
        {
            this.user = user;
            this.bizService = bizService;
            this.multiBusinessService = multiBusinessService;
            this.subLibBusinessService = subLibBusinessService;
            this.sysTenantService = sysTenantService;
        }
        /// <summary>
        /// 测试多租户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAll()
        {
            var res = await bizService.Query();
            return Ok(res);
        }

        /// <summary>
        /// 测试分表多租户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> MutiBusinessByTable()
        {
            var res = await multiBusinessService.Query();
            return Ok(res);
        }

        /// <summary>
        /// 分库多租户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> SubLibraryBusinessTable()
        {
            var res = await subLibBusinessService.Query();
            return Ok(res);
        }
        /// <summary>
        /// 多租户分库链接字符串
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> GetTenantCache()
        {
            return await sysTenantService.QueryWithCache();
        }

        ///// <summary>
        ///// 添加分库数据
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<object> AddTenant()
        //{
        //    return await sysTenantService.Add(new SysTenant()
        //    {
        //        Id = SnowFlakeSingle.instance.getID(),
        //        Name = "test name",
        //        TenantType = TenantTypeEnum.Db,
        //        ConfigId = "test config",
        //        Status = false
        //    });
        //}
    }
}
